package com.tools.cleanmaster.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.widget.RemoteViews;

import com.tools.cleanmaster.R;

@SuppressWarnings("static-access")
public class NotificationUtils {
    public static void cancelNotification(Context cxt, int notificationId) {
        NotificationManager nm = (NotificationManager) cxt.getSystemService(
                Context.NOTIFICATION_SERVICE);
        nm.cancel(notificationId);
    }

    /**
     * The notification has one big icon at left and two text lines at right.
     * The notification flag is set to Notification.FLAG_AUTO_CANCEL.
     * Notice: ticker 在Android L之后不再显示在statusBar
     */
    public static void notify(Context cxt, CharSequence ticker, CharSequence title, CharSequence message, PendingIntent pi, int notificationId) {
        notify(cxt, R.mipmap.ic_launcher, R.mipmap.ic_launcher, ticker, title, message, pi, notificationId, Notification.FLAG_AUTO_CANCEL, null);
    }

    public static void notify(Context cxt, CharSequence ticker, CharSequence title, CharSequence message, PendingIntent pi, int notificationId, int notificationFlags) {
        notify(cxt, R.mipmap.ic_launcher, R.mipmap.ic_launcher, ticker, title, message, pi, notificationId, notificationFlags, null);
    }

    public static void notify(Context cxt, int bigIconResId, int smallIconResId, CharSequence ticker,
            CharSequence title, CharSequence message, PendingIntent pi, int notificationId, int notificationFlags,
            RemoteViews views) {
        if (views == null) {
            views = new RemoteViews(cxt.getPackageName(), R.layout.notification_two_line_text);
            views.setImageViewResource(R.id.icon, bigIconResId);
            views.setTextViewText(R.id.title, title);
            views.setTextViewText(R.id.message, message);
        }

        Notification notification = new Notification();
        notification.flags = notificationFlags;
        notification.icon = smallIconResId;
        notification.tickerText = ticker;
        notification.contentView = views;
        notification.contentIntent = pi;
        notification.when = 0;
        notification.defaults = 0;
        notification.sound = null;
        notification.vibrate = null;

        NotificationManager nm = (NotificationManager) cxt.getSystemService(
                Context.NOTIFICATION_SERVICE);
        try {
            nm.notify(notificationId, notification);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
